package strategy

import (
	"fmt"
	"quantitative/model"
	"quantitative/position"
	"quantitative/common"
)

type Quantity interface {
	SignalBuy(shares model.Share) (bool)
	SignalCell(shares model.Share) (bool)
}

/**
双均线策略
 */
type TwinAverage struct {
	timeHelper common.TimeHelper
}

/**
购入信号
 */
func (t TwinAverage) SignalBuy(share model.Share) bool {
	if share.PreMa10 > share.PreMa20 {
		return true
	}
	return false
}

/**
卖出信号
 */
func (t TwinAverage) SignalCell(share model.Share) bool {
	if share.PreMa10 < share.PreMa20 {
		return true
	}
	return false
}

/**
交易数量
 */
func (t TwinAverage) TransNum() int64 {
	return 100
}

/**
策略执行
 */
func (t TwinAverage) Run(oneShare model.Share, holdPosition *position.HoldPosition) int64 {
	buy := t.SignalBuy(oneShare) //买入信号
	cell := t.SignalCell(oneShare) //卖出信号

	//持仓为空 and 买入信号
	if buy && holdPosition.Num <= 0 {
		//执行交易
		holdPosition.Buy(oneShare.Open, t.TransNum())

		fmt.Println(t.timeHelper.Format(oneShare.Timestamp)," | ", float64(oneShare.Open) / 100, "元购入")
	}

	//本次盈利
	var thisProfit int64 = 0

	//有持仓 and 卖出信号
	if cell && holdPosition.Num >= t.TransNum() {
		thisProfit = (oneShare.Open - holdPosition.Cost) * t.TransNum()

		fmt.Println(t.timeHelper.Format(oneShare.Timestamp)," | ",  float64(oneShare.Open) / 100, "元卖出 | 本次持仓盈利", float64(thisProfit)/ 100)

		//执行交易
		holdPosition.Cell(oneShare.Open, t.TransNum())
	}

	return thisProfit
}